//
//  ZTodoMeetingParticipateVC.m
//  WeClubs
//
//  Created by Zed Link on 12/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZTodoMeetingParticipateVC.h"
#import "ZParticipateModel.h"
#import "ZParticipateCell.h"
#import "ZCommentHeader.h"

static NSString * const ParticipateCellReuseID = @"ZParticipateCellReuseID";
static NSString * const CommentHeaderCellReuseID = @"ZCommentHeaderCellReuseID";

@interface ZTodoMeetingParticipateVC ()

/** 成员数据源 */
@property (strong, nonatomic) NSArray <ZParticipateModel *> *participateModels;

@property (weak, nonatomic) IBOutlet UITableView *mainView;

@end

@implementation ZTodoMeetingParticipateVC

+ (instancetype)zNew
{
    return [self zStoryboardInstance];
}

- (void)zConfigNavigation
{
    [super zConfigNavigation];
    
    [self zAddBackItemWithGesture:YES];
    self.navigationItem.title = @"会议参与详情";
    self.navigationItem.rightBarButtonItem = [self zNavItemWithTitle:@"导出Excel" andSelector:@selector(exportExcelAction)];
    
    [self zClearNavBgColorAndSeparator:NO];
    [self.navBgView setBackgroundColor:NAVBAR_BG_COLOR];
    [self.navSeparatorLine setImage:[UIImage imageNamed:@"shadow_nav"]];
}

- (void)exportExcelAction
{
    
}

#pragma mark - View Life Cycle

- (void)zLoadSubview
{
    [super zLoadSubview];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.mainView.rowHeight = UITableViewAutomaticDimension;
    self.mainView.estimatedRowHeight = 60;
    self.mainView.sectionHeaderHeight = 36;
    self.mainView.separatorColor = SEPARATOR_BG_COLOR;
    
    [self.mainView registerClass:[ZCommentHeader class] forHeaderFooterViewReuseIdentifier:CommentHeaderCellReuseID];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
        #warning Done
    
    NSDictionary *dataPara = @{
                               @"user_id" : @([ZUserInfo sharedInstance].userId),
                               @"token" : [ZUserInfo sharedInstance].token,
                               @"meeting_id" : @(self.meetingModel.meetingId),
                               };
    
    
    [HTTPRequest POST:kGet_meeting_participationUrl parameter:dataPara success:^(id resposeObject) {
       
        
        NSArray *participates = resposeObject[@"data"][@"participation"];
        
        NSMutableArray *temp = [NSMutableArray arrayWithCapacity:participates.count];
        for (NSDictionary *dict in participates) {
            ZParticipateModel *model = [[ZParticipateModel alloc] initWithDictionary:dict];
            [temp addObject:model];
        }
        self.participateModels = [temp copy];
    } failure:^(NSError *error) {
       
    }];
//    [[ZNetManager sharedInstance] updateMeetingParticipate:self.meetingModel.meetingId success:^(ZNetManagerStatus status, NSString *resultMsg, NSDictionary *data)
//    {
//        NSArray *participates = data[@"participation"];
//        
//        NSMutableArray *temp = [NSMutableArray arrayWithCapacity:participates.count];
//        for (NSDictionary *dict in participates) {
//            ZParticipateModel *model = [[ZParticipateModel alloc] initWithDictionary:dict];
//            [temp addObject:model];
//        }
//        self.participateModels = [temp copy];
//        
//    } failure:nil];
}


#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.participateModels.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZParticipateModel *participateModel = self.participateModels[indexPath.row];
    
    ZParticipateCell *cell = [tableView dequeueReusableCellWithIdentifier:ParticipateCellReuseID];
    cell.participateModel = participateModel;
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    ZCommentHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CommentHeaderCellReuseID];
    header.style = ZTableHeaderStyleComment;
    header.alignment = ZTableHeaderAlignmentCenter;
    
    NSDictionary *attr = @{
                           NSFontAttributeName : [UIFont systemFontOfSize:13 weight:UIFontWeightLight],
                           NSForegroundColorAttributeName : [UIColor zColorWithHex:0x666666],
                           };
    [header.descriptionBtn setAttributedTitle:[[NSAttributedString alloc] initWithString:@"会议参与人员" attributes:attr] forState:UIControlStateNormal];
    
    return header;
}

#pragma mark - Getter & Setter

- (void)setParticipateModels:(NSArray<ZParticipateModel *> *)participateModels
{
    _participateModels = participateModels;
    [self.mainView reloadData];
}

@end
